package com.qiankan.qmsj.widget.bannerViewPager;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.qiankan.qmsj.R;


/**
 * ViewPagerIndicator helps the user recognize the position of current page.
 * 指示器帮助用户分辨出当前页所在的位置
 *
 * @author Chen Yu
 * @version 1.0.0
 * @date 2016-10-03
 */
public class ViewPagerIndicator extends LinearLayout {

    private Context mContext;
    private Paint mPaint;
    private View mMoveView;
    private int mCurrentPosition = 0;
    private float mPositionOffset;
    private int mItemCount = DRFAULT_ITEMCOUNT;
    private int mPadding = DEFAULT_PADDING;
    private int mRadius = DEFAULT_RADIUS;
    //the distance from the left side of the previous item to the left side of the next item.
    private int mDistanceBtwItem = mRadius * 2 + mPadding;

    private static final int DRFAULT_ITEMCOUNT = 5;
    private static final int DEFAULT_RADIUS = 10;
    private static final int DEFAULT_PADDING = 10;

    public ViewPagerIndicator(Context context) {
        this(context, null);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    private void init() {
        setOrientation(LinearLayout.HORIZONTAL);
        setWillNotDraw(false);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.GRAY);

        // mMoveView = new MoveView(mContext);
        //   mMoveView = LayoutInflater.from(mContext).inflate(R.layout.banner1_item,null);
//        LayoutParams lp;
//        lp= (LayoutParams) mMoveView.getLayoutParams();
//        lp.width=50;
//        lp.height=30;
//        mMoveView.setLayoutParams(lp);
        mMoveView = new ImageView(mContext);
//        mMoveView.setBackgroundResource(R.mipmap.banner1);
        mMoveView.setBackgroundResource(R.drawable.white_cycle_dot);
        addView(mMoveView);
    }

    public void setItemCount(int count) {
        this.mItemCount = count;
        requestLayout();
    }

    public void setRadius(int radius) {
        this.mRadius = radius;
        this.mDistanceBtwItem = mRadius * 2 + mPadding;
        requestLayout();
    }

    public void setPadding(int padding) {
        this.mPadding = padding;
        this.mDistanceBtwItem = mRadius * 2 + mPadding;
        requestLayout();
    }

    public void setPositionAndOffset(int position, float offset) {
        this.mCurrentPosition = position;
        this.mPositionOffset = offset;
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mPadding + (mRadius * 2 + mPadding) * mItemCount, 2 * mRadius + 2 * mPadding);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        /*mMoveView.layout(
                (int) (mPadding + mDistanceBtwItem * (mCurrentPosition + mPositionOffset)) - mRadius,
                mPadding,
                (int) (mDistanceBtwItem * (1 + mCurrentPosition + mPositionOffset)) + 10,
                mPadding + mRadius * 2);*/
        mMoveView.layout(
                (int) (mPadding + mDistanceBtwItem * (mCurrentPosition + mPositionOffset)),
                mPadding,
                (int) (mPadding + mDistanceBtwItem * (mCurrentPosition + mPositionOffset) + 2 * mRadius),
                mPadding + 2 * mRadius);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < mItemCount; i++) {
            canvas.drawCircle(mRadius + mPadding + mRadius * i * 2 + mPadding * i,
                    mRadius + mPadding, mRadius - 2, mPaint);
        }
    }

    private class MoveView extends View {
        private Paint mPaint;

        public MoveView(Context context) {
            super(context);
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.argb(255, 255, 176, 93));
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            setMeasuredDimension(mRadius * 2, mRadius * 2);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            //  canvas.drawCircle(mRadius,mRadius,mRadius,mPaint);

            canvas.drawColor(Color.WHITE);                  //白色背景
            mPaint.setStrokeWidth((float) 3.0);              //线宽
            mPaint.setStyle(Paint.Style.STROKE);                   //空心效果
            Rect r1 = new Rect();                         //Rect对象
            r1.left = 50;                                 //左边
            r1.top = 50;                                  //上边
            r1.right = 50;                                   //右边
            r1.bottom = 50;                              //下边
            canvas.drawRect(r1, mPaint);                 //绘制矩形
            RectF r2 = new RectF();                           //RectF对象
            r2.left = 50;                                 //左边
            r2.top = 50;                                 //上边
            r2.right = 50;                                   //右边
            r2.bottom = 50;                              //下边
            canvas.drawRoundRect(r2, 10, 10, mPaint);
        }
    }
}
